﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

namespace ZhhbSoft.Data
{
    /// <summary>
    ///MsSqlDbProvider 的摘要说明
    /// </summary>
    public class MsSqlDbProvider : DbProvider
    {
        public MsSqlDbProvider()
            : this("")
        {
        }
        public MsSqlDbProvider(string strConnectionString)
            : base(strConnectionString)
        {
            _supportTRANSACTION = true;
        }

        public override string CreateConnectionString(params string[] strItems)
        {
            if (strItems.Length <= 0)
            {
                return "";
            }
            if (strItems.Length == 1)
            {
                if (strItems[0].IndexOf(";") > 0)
                {
                    return strItems[0];
                }
                return string.Format("server={0}", strItems[0]);
            }
            if (strItems.Length == 2)
            {
                return string.Format("server={0};uid={1}", strItems[0], strItems[1]);
            }
            if (strItems.Length == 3)
            {
                return string.Format("server={0};uid={1};pwd={2}", strItems[0], strItems[1], strItems[2]);
            }
            if (strItems.Length == 4)
            {
                return string.Format("server={0};uid={1};pwd={2};database={3}", strItems[0], strItems[1], strItems[2], strItems[3]);
            }
            return string.Format("server={0};uid={1};pwd={2};database={3};Connection Timeout={4}", strItems[0], strItems[1], strItems[2], strItems[3], strItems[4]);
        }

        protected override IDbConnection CreateConnection()
        {
            try
            {
                var conn = new SqlConnection();
                return conn;
            }
            catch
            {
                throw new Exception("数据库连接失败！");
            }

        }
        public override string DbDateFunc
        {
            get
            {
                return "getdate()";
            }
        }

        protected override DbDataAdapter CreateDataAdapter(IDbCommand cmd)
        {
            return new SqlDataAdapter((SqlCommand)cmd);
        }

        protected override IDbDataParameter CreateDataParameter()
        {
            var sqlParameter = new SqlParameter();
            return sqlParameter;
        }
        protected override IDbDataParameter[] DiscoverSpParameterSet(string spName, bool includeReturnValueParameter)
        {
            using (var cn = (SqlConnection)CreateConnection())
            {
                cn.ConnectionString = StrConnectionString;
                cn.Open();
                using (var cmd = new SqlCommand(spName, cn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlCommandBuilder.DeriveParameters(cmd);
                    if (!includeReturnValueParameter)
                    {
                        cmd.Parameters.RemoveAt(0);
                    }
                    var discoveredParameters = new IDbDataParameter[cmd.Parameters.Count];
                    cmd.Parameters.CopyTo(discoveredParameters, 0);
                    return discoveredParameters;
                }
            }
        }
        public override int ExecuteNonQuery(IDbTransaction transaction, IDbCommand cmd, out object returnValue)
        {
            IDataParameter dataParameter = CreateDataParameter("@" + Guid.NewGuid().ToString("N"), DbType.Int32, ParameterDirection.ReturnValue);

            cmd.Parameters.Add(dataParameter);
            int rowAffected = base.ExecuteNonQuery(transaction, cmd, out returnValue);
            returnValue = dataParameter.Value;

            return rowAffected;
        }
        public override long DataFileSize
        {
            get
            {
                return Convert.ToInt64(ExecuteScalar("select sum(size)* 8192 from sysfiles"));
            }
        }

        public override void ChangeDatabase(string dbName)
        {
            ExecuteNonQuery("use " + dbName);
        }
        public override IDbCommand CreateCommand()
        {
            return new SqlCommand();
        }
    }
}